﻿/*
*   报表数据加载
*/
var intervalID = setInterval('freshChart()', 10000); //一小时执行一次
var iData = null;


//XML文件报表样式
var xmlStyleStr = ' <styles>';
xmlStyleStr += ' <definition>';
xmlStyleStr += '        <style name="myCaptionFont" type="font" font="Arial" size="14" color="666666" bold="1"/>';
xmlStyleStr += '        <style name="myLabelsFont" type="font" font="Arial" size="11" bold="0" underline="0"/>';
xmlStyleStr += '        <style name="myLegendFont" type="font" size="11"/>';
xmlStyleStr += '        <style name="myToolTipFont" type="font" font="Arial" size="11"/>';
xmlStyleStr += '     </definition>';
xmlStyleStr += '     <application>';
xmlStyleStr += '        <apply toObject="Caption" styles="myCaptionFont" />';
xmlStyleStr += '        <apply toObject="DataLabels" styles="myLabelsFont" />';
xmlStyleStr += '        <apply toObject="Legend" styles="myLegendFont" />';
xmlStyleStr += '        <apply toObject="ToolTip" styles="myToolTipFont" />';
xmlStyleStr += '     </application>';
xmlStyleStr += '   </styles>';


var p = {
    title: '基站信道占用率', //报表标题
    units: '%', //报表单位
    showValues: 0, //是否显示值
    showLegend: 0, //是否显示图例
    type: 'TBS'//报表类型，DXT(交换机),TBS(基站)
}
/*
*   页面初始化加载
*/
function initPage() {
    initParam(); //初始化参数
    if (getData())//;//获取数据
    {
        createChart(); //加载统计图
    }
}

/*
*   初始化统计参数
*/
function initParam() {
    var o = _('check_show');
    if (o.checked) {
        p.showValues = 1;
    }
}

/*
*   将数据集转化成XML文件格式
*   @param  p   显示图形基本参数对象
*/
function setXMl(p, data) {
    try {
        //XML文件报表标题
        var xmlTitleStr = '<chart caption="' + p.title + '" numberSuffix="' + p.units + '" showValues="' + p.showValues + '" showLegend="' + p.showLegend + '"';
        xmlTitleStr += '  bgColor="#FFFFFF" canvasBgAlpha="100" yAxisMinValue="0" yAxisMaxValue="100" formatNumberScale="0" canvasBorderThickness="0" chartRightMargin ="25" showYAxisValues="1"  showXAxisValues="0" numDivLines="1" showLabels ="1">';
        //XML报表文件
        var xmlStr = '<dataset seriesName="基站信道占用率">';
        //存储标题
        var cap = [];
        var count = 10;
        var equipName = '';
        var tmpEquipName = '';
        var isSame = true;
        var length = data.length;
        for (var i = 0; i < length; i++) {
            tmpEquipName = data[i].NODENAME;
            cap.push(tmpEquipName);
            xmlStr += ' <set value="' + parseFloat(data[i].UTILIZATION) + '"  tooltext="' + tmpEquipName + '{br}信道占用率:' + data[i].UTILIZATION + '"/> ';
        }
        xmlStr += "</dataset>";
        //初始化X标题
        var xmlCaptionStr = '<categories>';
        var length = cap.length;
        for (var i = 0; i < length; i++) {
            xmlCaptionStr += ' <category label="' + cap[i] + '" />';
        }
        xmlCaptionStr += ' </categories>';

        return xmlTitleStr + xmlCaptionStr + xmlStr + xmlStyleStr + '  </chart>';
    }
    catch (e) {
        alert(e);
        return '<chart ></chart>';
    }
}

/*
*   获取数据
*/
function getData() {
    var sTime = _('StartDate').value; //开始时间
    var eTime = _('EndDate').value; //结束时间
    if (sTime > eTime) {
        alert('开始时间不能大于结束时间，请重新选择时间!');
        _('StartDate').focus();
        return false;
    }
    var top = _('txtTOP').value;
    try {
        if (parseInt(top) < 0) {
            alert('TOP数目不能小于0!');
            _('txtTOP').focus();
            return false;
        }
    } catch (e) {
        alert('输入的只能为整数!');
        _('txtTOP').focus();
        return false;
    }
    var neidList = window.parent.GetCookie('zhwg_TBSXDZYL');
    if (neidList) {
        if (neidList.lastIndexOf(',') == neidList.length - 1) {
            neidList = neidList.substring(0, neidList.length - 1);
        }
        neidList = '\'' + neidList.replace(';', '').replace(/,/g, '\',\'') + '\'';
    }
    else {
        neidList = '';
    }
    iData = Topo_ImpMonitorIndex_XDZYL.GetModelList(top, neidList).value;
    return true;
    //    if (p.type == "TBS") {
    //        iData = Monitor_ReportDXT.GetTBS(sTime, eTime, "20").value;
    //        return true;
    //    }
    //    if (p.type == "DXT") {
    //        iData = Monitor_ReportDXT.GetModelGroup(sTime, eTime, "0").value;
    //        iData = dataChange();
    //        return true;
    //    }
}

/*
*   转化数据集，对于交换机，需要将原数据集转化
*/
function dataChange() {
    var name = ''; //话务量分类名称
    var id = '';
    var idTmp = '';
    var m = 0;
    var tData, data;
    var tDatas = [];
    //将组呼话务量转化
    if (iData) {
        var length = iData.Rows.length;
        for (var i = 0; i < length; i++) {
            if (0 == i) {
                id = iData.Rows[i].ne_id;
                tData = {
                    id: iData.Rows[i].ne_id,
                    name: iData.Rows[i].equip_name + '组呼',
                    type: 'group',
                    max: 0,
                    data: []
                }
                data = {
                    time: '',
                    value: ''
                };
            }
            m = parseFloat(iData.Rows[i].hwl);
            if (m > tData.max) {
                tData.max = m;
            }
            data = {
                time: new Date(iData.Rows[i].new_time_code.replace(/-/, "/")).format("dd日hh时"),
                value: m//将各指标的数据相加
            }
            if (id != iData.Rows[i].ne_id || length == i + 1) {
                if (length == i + 1) { tData.data.push(data); }
                id = iData.Rows[i].ne_id;
                tDatas.push(tData);
                tData = {
                    id: iData.Rows[i].ne_id,
                    name: iData.Rows[i].equip_name + '组呼',
                    type: 'group',
                    max: 0,
                    data: []
                }
                data = {
                    time: '',
                    value: ''
                };
            }
            tData.data.push(data);
        }
    }
    return tDatas;
}

/*
*   是否显示柱状图的数据
*/
function updateChart() {
    var o = _('check_show');
    p.showValues = 0;
    if (o.checked) {
        p.showValues = 1;
    }
    //getChartFromId('ReportChart').setDataXML(setXMl(p));
    updateChartSWF();
}
/*
*   动态更改统计图样式
*/
function updateChartSWF() {
    var o = _('s_chartType');
    var chartSWF = "../JS/FusionCharts/swf/";
    chartSWF += o.value + '.swf';
    createChart(chartSWF);
}

/*
*   创建统计图形
*/
function createChart(chartSWF) {
    if (!chartSWF) {//如果没有传入参数，就选择一个默认值
        chartSWF = "../JS/FusionCharts/swf/MSColumn3D.swf";
    }

    var length = iData.length;
    if (length <= 20) {
        var myChart = new FusionCharts(chartSWF + "?ChartNoDataText=没有数据!&LoadDataErrorText=数据错误!", "ReportChart0", "1000", "400", "0", "1");
        myChart.setDataXML(setXMl(p, iData));
        myChart.render("div_report0");
    }
    else {
        var myChart;
        var count = 0;
        //如果最后一个大于5，就重新添加一个，否则就直接镶嵌在前面的统计图中
        count = parseInt(length % 15 > 5 ? (length / 15 + 1) : length / 15);
        for (var i = 0; i < count; i++) {
            myChart = new FusionCharts(chartSWF + "?ChartNoDataText=没有数据!&LoadDataErrorText=数据错误!", "ReportChart" + i, "1000", "400", "0", "1");
            $('<div>').attr({ 'id': 'div_report' + i }).appendTo($('body'));
            if (i == count - 1) {
                myChart.setDataXML(setXMl(p, iData.slice(i * 15, iData.length)));
            }
            else {
                myChart.setDataXML(setXMl(p, iData.slice(i * 15, (i + 1) * 15)));
            }
            myChart.render('div_report' + i);
        }
    }
}

/*
*   查询按钮事件
*/
function clickInit() {
    if (getData())//;//获取数据
    {
        createChart(); //加载统计图
    }
}

/*
*   设置界面更新时间
*/
function setUpdateTime() {
    var o = _('updateTime');
    clearInterval(intervalID); //清楚时间刷新
    initPage();
    intervalID = setInterval('freshChart()', o.value);
}

function freshChart() {
    getData();
    var cs = _('check_show');
    p.showValues = 0;
    if (cs.checked) {
        p.showValues = 1;
    }
    var o = _('s_chartType');
    var chartSWF = "../JS/FusionCharts/swf/";
    chartSWF += o.value + '.swf';
    createChart(chartSWF);
}